package com.wyj.utils;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.taobao.api.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * @author ：wyj
 * @description：TODO
 * @date ：2024/3/8 12:08 AM
 */
public class ErrorUtils {
    private static final String ERROR_TEST_DING_URL = "";


    public static void sendErrorMessage(Exception e, String message)  {
        DingTalkClient client = new DefaultDingTalkClient(ERROR_TEST_DING_URL);
        OapiRobotSendRequest request = new OapiRobotSendRequest();

        request.setMsgtype("text");
        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
        StringBuilder sb = new StringBuilder();
        sb.append("测试服报错:").append("\n");
        // 获取到报错的类跟位置
        String stackTrace = ExceptionUtils.getStackTrace(e);
        int index = stackTrace.indexOf(")") + 1;
        String substring = StringUtils.substring(stackTrace, 0, index);
        sb.append(substring).append("\n");
        sb.append(message).append("\n");
        System.out.println("测试服报错"+sb.toString());
        text.setContent(sb.toString());
        request.setText(text);
        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
        // isAtAll类型如果不为Boolean，请升级至最新SDK
        at.setIsAtAll(true);
        request.setAt(at);
        try {
            client.execute(request);
        } catch (Exception exception) {
            throw new RuntimeException(" dingDing error");
        }

    }
}
